<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Http\Util\Status;
use App\Model\InfoType;

trait ServiceInfoType{

    /**
     * czs
     * @return mixed
     * 资讯类型列表
     */
    public static function getList($keyWord,$limit){
        if (empty($keyWord)) {
            $list = InfoType::orderBy('id','desc')->paginate($limit);
        } else {
            $list = InfoType::orderBy('id','desc')->where("name","like", '%'.$keyWord.'%')->paginate($limit);
        }
        return Result::success()->setData($list);
    }

    /**
     * 获取启用列表
     * @author gaoyoulong
     * @return mixed
     */
    public static function getAbledList(){
        return InfoType::where('status',Status::ON)->get();
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = InfoType::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $res = InfoType::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = InfoType::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        InfoType::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * 资讯类型
     * czs
     */
    public static function typePull() {
        $res = InfoType::where('status',1)->select('id', 'name')->get()->toarray();
        return Result::success()->setData($res);
    }
}


